package com.example.jsr_study.utils;

import lombok.Getter;

/**
 * 自定义响应结构
 *
 * @author Journey
 * @version 创建时间：2020年2月3日 下午3:45:53
 * @ClassName 类名称
 * @Description 类描述
 */
@Getter
public class Result<T> {

    /**
     * 请求成功
     */
    public final static int CODE_SUCCESS = 200;
    public final static String MSG_SUCCESS = "操作成功";

    /**
     * 结果编码(默认: 200)
     */
    private int resultCode;

    /**
     * 结果描述(默认: 操作成功)
     */
    private String resultMsg;

    /**
     * 数据集
     */
    private T data;

    private int showType = DEFAULT_SHOW_TYPE;
    private static final int DEFAULT_SHOW_TYPE = 0;
    private static final int WARN_SHOW_TYPE = 1;
    private static final int ERROR_SHOW_TYPE = 2;

    private Result() {
    }

    private Result(int resultCode, String resultMsg, T data) {
        this.resultCode = resultCode;
        this.resultMsg = resultMsg;
        this.data = data;
    }

    public Result(int resultCode, String resultMsg, int showType) {
        this.resultCode = resultCode;
        this.resultMsg = resultMsg;
        this.showType = showType;
    }

    private Result(T data) {
        this.resultCode = CODE_SUCCESS;
        this.resultMsg = MSG_SUCCESS;
        this.data = data;
    }

    public static <T> Result<T> ok(T data) {
        return new Result<>(data);
    }

    public static <T> Result<T> ok() {
        return new Result<>(Result.CODE_SUCCESS, Result.MSG_SUCCESS, null);
    }


    public static <T> Result<T> fail(String massage) {
        Result<T> ret = new Result<>(DEFAULT_SHOW_TYPE, massage, null);
        ret.showType = WARN_SHOW_TYPE;
        return ret;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

}
